capture log close
log using "results", replace
log on

clear

*Set working directory path
cd "C:\Users\username\filepath"
import delimited "data.csv", case(preserve) clear

label variable question "In any order, question=1 means it is question G1; question=2 means it is question G2; question=3 means it is question O1; question= 4 means it is question O2"
label variable decision "decision=1: choosing late; decision=0: choosing early; decision=0.5: indifference; decision=.: subjects did not answer the questions"

sort Subject Order
drop if decision==.  //delete observations that are incomplete (3 subjects = 12 observations deleted)

gen questionN="" // this variable captures the alphabet name of the question. 

replace questionN="G1" if question==1
replace questionN="G2" if question==2
replace questionN="O1" if question==3
replace questionN="O2" if question==4

gen int switchp=. // number of cents required for switching choice.
replace switchp=1 if switch=="Q1"
replace switchp=5 if switch=="Q2"
replace switchp=10 if switch=="Q3"
replace switchp=15 if switch=="Q4"
replace switchp=20 if switch=="Q5"
replace switchp=25 if switch=="Q6"
replace switchp=30 if switch=="Q7"
replace switchp=35 if switch=="Q8"
replace switchp=40 if switch=="Q9"
replace switchp=50 if switch=="Q10"

*******************Replicate Figure 1, Section 5**********************
sort question switchp
by question: tabulate switchp, missing
by question: tabulate switchp, missing plot
sort Subject Order

*******************Replicate Table 1, Section 5***********************
gen early=1-decision //early=1: Choosing early resolution of uncertainty, early=0: Choosing late resolution of uncertainty

version 15: table questionN if early!=. & early!=0.5, c (freq mean early) // only include questions with strict preferences

prtest early==0.5 if early!=. & early!=0.5 & questionN=="G1" // one-sample test of proportions.
prtest early==0.5 if early!=. & early!=0.5 & questionN=="G2"
prtest early==0.5 if early!=. & early!=0.5 & questionN=="O1"
prtest early==0.5 if early!=. & early!=0.5 & questionN=="O2"



*******************Replicate Table 2, Section 6*******************

gen TempG1=decision if questionN=="G1" //this is a temporary variable used later. 
gen TempG2=decision if questionN=="G2"
gen TempO1=decision if questionN=="O1"
gen TempO2=decision if questionN=="O2"

by Subject: egen decisionG1=max(TempG1) // decisionG1 is the decision in question G1
by Subject: egen decisionG2=max(TempG2) // decisionG2 is the decision in question G2
by Subject: egen decisionO1=max(TempO1) // decisionO1 is the decision in question O1
by Subject: egen decisionO2=max(TempO2) // decisionO2 is the decision in question O2

drop TempG1 TempG2 TempO1 TempO2 //dropping variables that are no longer necessary

gen decisionG1N="" // meaning of variable decisionG1 in English
replace decisionG1N="Early" if decisionG1==0
replace decisionG1N="Late" if decisionG1==1
replace decisionG1N="Indif." if decisionG1==0.5
replace decisionG1N="NoAnswer" if decisionG1==.

gen decisionG2N=""
replace decisionG2N="Early" if decisionG2==0
replace decisionG2N="Late" if decisionG2==1
replace decisionG2N="Indif." if decisionG2==0.5
replace decisionG2N="NoAnswer" if decisionG2==.


gen decisionO1N=""
replace decisionO1N="Early" if decisionO1==0
replace decisionO1N="Late" if decisionO1==1
replace decisionO1N="Indif." if decisionO1==0.5
replace decisionO1N="NoAnswer" if decisionO1==.


gen decisionO2N=""
replace decisionO2N="Early" if decisionO2==0
replace decisionO2N="Late" if decisionO2==1
replace decisionO2N="Indif." if decisionO2==0.5
replace decisionO2N="NoAnswer" if decisionO2==.

egen Answer = concat(decisionG1N decisionG2N decisionO1N decisionO2N), punct(-) // This variable represents answers in all four questions
tabulate Answer, sort



*******************Replicate Table 3, Appendix B*******************
gen AppearOrder="" // this variable captures the order that a question appears. "1st" means the question appears first and so on.

replace AppearOrder="1st" if Order=="T 1-2-3-4" & questionN=="G1"
replace AppearOrder="2nd" if Order=="T 1-2-3-4" & questionN=="G2"
replace AppearOrder="3rd" if Order=="T 1-2-3-4" & questionN=="O1"
replace AppearOrder="4th" if Order=="T 1-2-3-4" & questionN=="O2"

replace AppearOrder="1st" if Order=="T 2-1-4-3" & questionN=="G2"
replace AppearOrder="2nd" if Order=="T 2-1-4-3" & questionN=="G1"
replace AppearOrder="3rd" if Order=="T 2-1-4-3" & questionN=="O2"
replace AppearOrder="4th" if Order=="T 2-1-4-3" & questionN=="O1"

replace AppearOrder="1st" if Order=="T 3-4-1-2" & questionN=="O1"
replace AppearOrder="2nd" if Order=="T 3-4-1-2" & questionN=="O2"
replace AppearOrder="3rd" if Order=="T 3-4-1-2" & questionN=="G1"
replace AppearOrder="4th" if Order=="T 3-4-1-2" & questionN=="G2"

replace AppearOrder="1st" if Order=="T 4-3-2-1" & questionN=="O2"
replace AppearOrder="2nd" if Order=="T 4-3-2-1" & questionN=="O1"
replace AppearOrder="3rd" if Order=="T 4-3-2-1" & questionN=="G2"
replace AppearOrder="4th" if Order=="T 4-3-2-1" & questionN=="G1"

version 15: table questionN AppearOrder if early!=. & early!=0.5, c (mean early)


*******************Replicate Table 4, Appendix B*******************
replace decision=0 if decision==0.5 // treating indifferent subjects as choosing early to use logistic regression 

gen OrderN=1 
replace OrderN=2 if Order=="T 2-1-4-3"
replace OrderN=3 if Order=="T 3-4-1-2"
replace OrderN=4 if Order=="T 4-3-2-1"

logit decision i.question i.OrderN, cluster(Subject) //clustering at subject level


*******************Replicate Tables 5-8, Appendix B*******************

keep if question==1 // Keeping one observation per subject as we already defined a variable capturing answers in each question.

//Replicate Table 5, Appendix B
tabulate decisionG1 decisionG2
signrank decisionG1=decisionG2

//Replicate Table 6, Appendix B
tabulate decisionG1 decisionO1
signrank decisionG1=decisionO1

//Replicate Table 7, Appendix B
tabulate decisionG2 decisionO2
signrank decisionG2=decisionO2


//Replicate Table 8, Appendix B
tabulate decisionO1 decisionO2
signrank decisionO1=decisionO2


log close
translate results.smcl results.pdf
